/**
 * Copyright (c) 2010-2011 Pierre Labatut
 *
 * This file is part of Space-Chronicles project.
 *
 * Space-Chronicles is free software: you can redistribute it and/or modify it
 * under the terms of its license which may be found in the accompanying
 * LICENSE file or <http://code.google.com/p/space-chronicles/>.
 */

package space.model;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import space.stream.StructureInput;

/**
 *
 * @author Pierre
 */
public class MdlLibrary {
    // Groups are collections of object that can be instanciated several times
    final private MdlGroup groups[];
    // Roots are objects
    final private MdlObject roots[];

    // Roots are objects
    final private Material materials[];

    public MdlLibrary(StructureInput di) {
        // Skip the format string
        di.readString();

        groups = di.readArray(MdlGroup.class);
        roots = di.readArray(MdlObject.class);
        materials = di.readArray(Material.class);
    }

     public List<MdlObject> getRoots() {
      return Collections.unmodifiableList(Arrays.asList(roots));


    }
    public MdlObject getRoot(String rootName) {
        for (MdlObject object : roots) {
            if (rootName.equals(object.name)) {
                return object;
            }
        }
        throw new RuntimeException("No root object named '" + rootName + "' found in the library");
    }

    public Map<String, Drawable> getGroupLib() {
        final Map<String, Drawable> lib = new HashMap<String, Drawable>();
        for (MdlGroup g : groups) {
            lib.put(g.name, g.roots[0]);
        }
        return lib;
    }

    public Map<String, Material> getMaterialLib() {
        final Map<String, Material> lib = new HashMap<String, Material>();
        for (Material m :materials) {
            lib.put(m.name, m);
        }
        return lib;
    }
}
